package com.lc.YYGH.msm.controller;

import com.alibaba.excel.util.StringUtils;
import com.lc.YYGH.common.result.Result;
import com.lc.YYGH.msm.service.MsmService;
import com.lc.YYGH.msm.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/msm")
@Api(tags="短信服务接口")

public class MsmApiController {
    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @ApiOperation("发送手机验证码")
    @GetMapping("send/{phone}")
    public Result sendCode(@PathVariable String phone)
    {
        String code=redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code))
        {
            return Result.success();
        }
        code= RandomUtil.getSixBitRandom();
        boolean b=msmService.send(phone,code);
        if(b){
            redisTemplate.opsForValue().set(phone,code,2, TimeUnit.MINUTES);
            return Result.success();
        }
        return Result.build(509,"发送验证码失败");

    }
}
